package com.yoyo.ad.confusion;

import android.content.Context;

import com.yoyo.ad.api.YoYoConfig;
import com.yoyo.ad.utils.SPUtils;
import com.yoyo.ad.utils.StringUtil;
import com.yoyo.ad.utils.TimeUtils;


public class AdLocalSourceRecord {

    private static final String FILE_NAME = "ad_config";
    private static final int UPLOAD_INTERVAL = 60;

    public static int getLocalSourceCount(Context context, int sourceId) {
        return (int) SPUtils.get(context, String.valueOf(sourceId).concat("_count"), 0, FILE_NAME);
    }

    public static long getLocalSourceTime(Context context, int sourceId) {
        return (long) SPUtils.get(context, String.valueOf(sourceId).concat("_time"), 0L, FILE_NAME);
    }

    public static boolean canUpload(Context context) {
        return System.currentTimeMillis() - ((long) SPUtils.get(context, "upload", 0L, FILE_NAME)) > UPLOAD_INTERVAL * 1000;
    }

    public static void setUploadTime(Context context) {
        SPUtils.put(context, "upload", System.currentTimeMillis(), FILE_NAME);
    }

    //是否清理过数据
    public static boolean isDeleteData(Context context, String data) {
        return SPUtils.get(context, "delete_data", "", FILE_NAME).equals(data);
    }

    public static void setDeleteData(Context context, String data) {
        SPUtils.put(context, "delete_data", data, FILE_NAME);
    }

    //是否展示过banner
    public static boolean isBannerChange(Context context) {
        return System.currentTimeMillis() - ((long) SPUtils.get(context, "banner_change", 0L, FILE_NAME)) > YoYoConfig.BANNER_CHANGE_TIME_INTERVAL * 1000;
    }

    public static void setBannerChange(Context context) {
        SPUtils.put(context, "banner_change", System.currentTimeMillis(), FILE_NAME);
    }

    public static boolean canRequestIpData(Context context, int interval) {
        return System.currentTimeMillis() - ((long) SPUtils.get(context, "request_ip_data", 0L, FILE_NAME)) > interval * 1000;
    }

    public static void setRequestIpDataTime(Context context) {
        SPUtils.put(context, "request_ip_data", System.currentTimeMillis(), FILE_NAME);
    }

    public static void setLocalSource(Context context, int sourceId) {
        setLocalSourceTime(context, sourceId);
        setLocalSourceCount(context, sourceId);
    }

    public static void setLocalSourceCount(Context context, int sourceId) {
        SPUtils.put(context, String.valueOf(sourceId).concat("_count"), (int) SPUtils.get(context, String.valueOf(sourceId).concat("_count"), 0, FILE_NAME) + 1, FILE_NAME);
    }

    public static void setLocalSourceTime(Context context, int sourceId) {
        if (!TimeUtils.isToday(getLocalSourceTime(context, sourceId))) {
            resetLocalSourceCount(context, sourceId);
        }
        SPUtils.put(context, String.valueOf(sourceId).concat("_time"), System.currentTimeMillis(), FILE_NAME);
    }

    public static void resetLocalSourceCount(Context context, int sourceId) {
        SPUtils.put(context, String.valueOf(sourceId).concat("_count"), 0, FILE_NAME);
    }

    public static boolean isAdConfigTime(Context context) {
        long getLuckRedTime = (long) SPUtils.get(context, "ad_config_time", 0L);
        return System.currentTimeMillis() - getLuckRedTime < YoYoConfig.CONFIG_TIME_INTERVAL;
//        return TimeUtils.isToday(getLuckRedTime);
    }

    public static long getAdConfigTime(Context context) {
        return (long) SPUtils.get(context, "ad_config_time", 0L);
    }

    public static void setAdConfigTime(Context context) {
        SPUtils.put(context, "ad_config_time", System.currentTimeMillis());
    }

    public static int getOvertime(Context context) {
        return (int) SPUtils.get(context, "overtime", 800);
    }

    public static void setOvertime(Context context, String overtime) {
        SPUtils.put(context, "overtime", Integer.parseInt(StringUtil.notNull(overtime, "0")));
    }

    public static int getProbability(Context context) {
        return (int) SPUtils.get(context, "probability", 4);
    }

    public static void setProbability(Context context, String probability) {
        SPUtils.put(context, "probability", Integer.parseInt(StringUtil.notNull(probability, "4")));
    }
}
